﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CERT.Services.DAL;
using CERT.Services;
using System.Web.Mvc;
//using GoogleMapsApi.Entities.Geocoding.Request;
//using GoogleMapsApi.Engine;
//using GoogleMapsApi.Entities.Geocoding.Response;

namespace CERT.Controllers
{
    public class VictimsController : ApiController
    {
        private INETServices Service;


        public VictimsController()
        {
            Service = new NETServices();

        }

        public VictimsController(INETServices service)
        {
            Service = service;

        }

        //[ValidateAntiForgeryToken]
        //[System.Web.Mvc.Authorize]
        //// [HttpGet("incid/{incid}/Victim")]
        //public LocationVictim GetNewVictim(int locationId)
        //{
        //    //get the incident?
        //    var locvic = new LocationVictim();
        //    var loc = Service.GetLocation(a => a.Id == locationId);
        //    locvic.LocationId = locationId;
        //    locvic.Location = loc;
        //    locvic.People_Victim = new People_Victim();
        //    return locvic;



        //}

        // GET api/Incident/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public IEnumerable<Victim> GetVictims(int id)
        {
            try
            {
                var incident = Service.GetVictims(a => a.LocationId == id);

                if (incident == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                return incident.AsEnumerable<Victim>();
            }
            catch (SystemException ex)
            { }
            
            var loc = new Victim();
            return new List<Victim>();
        }

        //// GET api/Incident/5
        //[ValidateAntiForgeryToken]
        //[System.Web.Mvc.Authorize]
        //public LocationVictim GetVictim(int id)
        //{

        //    var incident = Service.GetLocationVictim(a => a.Id == id);

        //    if (incident == null)
        //    {
        //        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        //    }
        //    return incident;
        //}

        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public HttpResponseMessage PutVictim(int id,  Victim victim)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            try
            {
                if (victim.Location == null)
                {
                    var loc = Service.GetLocation(a => a.Id == id);
                    var locvic = new Victim();
                    locvic.LocationId = id;
                    locvic.Location = loc;
                   
                     Service.UpdateVictim(locvic, id);
                }
                else
                    Service.UpdateVictim(victim, id);
                
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Victim
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public HttpResponseMessage PostVictim(Victim Victim)
        {

            if (Victim.Location == null)
            {
                var loc = Service.GetLocation(a => a.Id == Victim.LocationId);
                Victim.Location = loc;
            }

            //if (!ModelState.IsValid)
            //{
            //    return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            //}
            try
            {
                Service.AddVictim(Victim);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            catch (SystemException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
            }
            var response = Request.CreateResponse(HttpStatusCode.Created, Victim);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = Victim.Id }));
            return response;

        }

        // DELETE api/Incident/5
        [ValidateAntiForgeryToken]
        [System.Web.Mvc.Authorize]
        public System.Net.Http.HttpResponseMessage DeleteVictim(int id)
        {

           if (id == 0)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
            try
            {
                Service.DeleteVictim(id);
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, id);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}